package org.ing.sisval.util;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import org.ing.sisval.be.BEDepreciacion;

import org.ing.sisval.bl.BLDepreciacion;

import org.rx.cr.conf.Config;

public class UtilDepreciacion {

    private String defaultCategoria;
    private String defaultAntiguedad;
    private String defaultMaterial;
    private String defaultEstado;
    private int defaultValorEstado;

    private Config config;

    public UtilDepreciacion(Config config) {
        this.config = config;
    }

    public void recuperarDatosDepreciacion(int depreciacionid, JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval) {
        BEDepreciacion be = null;
        BLDepreciacion bl = null;
        try {
            bl = new BLDepreciacion(config);
            be = bl.buscar(depreciacionid);

            defaultSelectedDepreciacion(be.getCategoria().trim(), be.getAntiguedad(), be.getMaterial().trim(), be.getEstado().trim(), be.getValorestado(), jcbcat, jcbanti, jcbmat, jcbest, jcbval);

        } catch (Exception e) {
            Logger.getLogger(UtilDepreciacion.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public void defaultSelectedDepreciacion(String cat, String anti, String mat, String est, int val,
            JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval) {
        setDefaultCategoria(cat);
        setDefaultAntiguedad(anti);
        setDefaultMaterial(mat);
        setDefaultEstado(est);
        setDefaultValorEstado(val);

        rellenarCategoria(jcbcat, jcbanti, jcbmat, jcbest, jcbval, getDefaultCategoria(), getDefaultAntiguedad(), getDefaultMaterial(), getDefaultEstado(), getDefaultValorEstado());
    }

    public void selectDefaultCategoria(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval, String cat, String anti, String mat, String est, int val) {

        for (int i = 0; i < jcbcat.getItemCount(); i++) {
            BEDepreciacion object = (BEDepreciacion) jcbcat.getItemAt(i);
            if (i > 0 && object.toString().toLowerCase().trim().equals(cat.toLowerCase().trim())) {
                jcbcat.setSelectedItem(object);
            }
        }
    }

    public void selectDefaultAntiguedad(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {

        for (int i = 0; i < jcbanti.getItemCount(); i++) {
            BEDepreciacion object = (BEDepreciacion) jcbanti.getItemAt(i);
            if (i > 0 && object.toString().toLowerCase().equals(anti.toLowerCase())) {
                jcbanti.setSelectedItem(object);
            }
        }
    }

    public void selectDefaultMaterial(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {
        for (int i = 0; i < jcbmat.getItemCount(); i++) {
            BEDepreciacion object = (BEDepreciacion) jcbmat.getItemAt(i);
            if (i > 0 && object.toString().toLowerCase().trim().equals(mat.toLowerCase().trim())) {
                jcbmat.setSelectedItem(object);
            }
        }
    }

    public void selectDefaultEstado(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {
        for (int i = 0; i < jcbest.getItemCount(); i++) {
            BEDepreciacion object = (BEDepreciacion) jcbest.getItemAt(i);
            if (i > 0 && object.toString().toLowerCase().trim().equals(est.toLowerCase().trim())) {
                jcbest.setSelectedItem(object);

            }
        }
    }

    public void selectDefaultValorEstado(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {
        for (int i = 0; i < jcbval.getItemCount(); i++) {
            BEDepreciacion object = (BEDepreciacion) jcbval.getItemAt(i);
            if (i > 0 && (object.toString().equals(Integer.toString(val)))) {
                jcbval.setSelectedItem(object);

            }
        }
    }

    public void eventoComboCategoria(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval) {
        if (!jcbanti.isPopupVisible()) {
            if (jcbcat.getItemCount() > 1) {
                if (jcbcat.getSelectedIndex() > 0) {
                    BEDepreciacion object = (BEDepreciacion) jcbcat.getSelectedItem();
                    rellenarAntiguedad(jcbcat, jcbanti, jcbmat, jcbest, jcbval, getDefaultCategoria(), getDefaultAntiguedad(), getDefaultMaterial(), getDefaultEstado(), getDefaultValorEstado());
                } else if (jcbcat.getSelectedIndex() == 0) {
                    jcbanti.removeAllItems();
                    jcbmat.removeAllItems();
                    jcbest.removeAllItems();
                    jcbval.removeAllItems();

                    jcbanti.addItem(new BEDepreciacion());
                    jcbmat.addItem(new BEDepreciacion());
                    jcbest.addItem(new BEDepreciacion());
                    jcbval.addItem(new BEDepreciacion());
                }
            }
        }
    }

    public void eventoComboAntiguedad(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval) {
        if (!jcbmat.isPopupVisible()) {
            if (jcbanti.getItemCount() > 1) {
                if (jcbanti.getSelectedIndex() > 0 && jcbcat.getSelectedIndex() > 0) {
                    BEDepreciacion object = (BEDepreciacion) jcbanti.getSelectedItem();

                    rellenaMaterial(jcbcat, jcbanti, jcbmat, jcbest, jcbval, getDefaultCategoria(), getDefaultAntiguedad(), getDefaultMaterial(), getDefaultEstado(), getDefaultValorEstado());
                } else if (jcbanti.getSelectedIndex() == 0) {
                    jcbmat.removeAllItems();
                    jcbest.removeAllItems();
                    jcbval.removeAllItems();

                    jcbest.addItem(new BEDepreciacion());
                    jcbval.addItem(new BEDepreciacion());
                    jcbmat.addItem(new BEDepreciacion());
                }
            }
        }
    }

    public void eventoComboMaterial(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval) {
        if (!jcbest.isPopupVisible()) {
            if (jcbmat.getItemCount() > 1) {
                if (jcbmat.getSelectedIndex() > 0 && jcbanti.getSelectedIndex() > 0 && jcbcat.getSelectedIndex() > 0) {
                    BEDepreciacion obj = (BEDepreciacion) jcbmat.getSelectedItem();

                    rellenaEstado(jcbcat, jcbanti, jcbmat, jcbest, jcbval, getDefaultCategoria(), getDefaultAntiguedad(), getDefaultMaterial(), getDefaultEstado(), getDefaultValorEstado());
                } else if (jcbmat.getSelectedIndex() == 0) {

                    jcbest.removeAllItems();
                    jcbval.removeAllItems();

                    jcbval.addItem(new BEDepreciacion());
                    jcbest.addItem(new BEDepreciacion());
                }
            }
        }
    }

    public void eventoComboEstado(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval) {
        if (!jcbval.isPopupVisible()) {
            if (jcbest.getItemCount() > 1) {
                if (jcbcat.getSelectedIndex() > 0 && jcbanti.getSelectedIndex() > 0 && jcbmat.getSelectedIndex() > 0 && jcbest.getSelectedIndex() > 0) {
                    BEDepreciacion obj = (BEDepreciacion) jcbest.getSelectedItem();
                    rellenaValorEstado(jcbcat, jcbanti, jcbmat, jcbest, jcbval, getDefaultCategoria(), getDefaultAntiguedad(), getDefaultMaterial(), getDefaultEstado(), getDefaultValorEstado());
                           

                } else if (jcbest.getSelectedIndex() == 0) {
                    jcbval.removeAllItems();
                    jcbval.addItem(new BEDepreciacion());
                }
            }
        }
    }

    public void eventoComboValorEstado(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval) {
        if (!jcbest.isPopupVisible()) {
            if (jcbval.getItemCount() > 1) {
                if (jcbcat.getSelectedIndex() > 0 && jcbanti.getSelectedIndex() > 0 && jcbmat.getSelectedIndex() > 0 && jcbest.getSelectedIndex() > 0 && jcbval.getSelectedIndex() > 0) {
                    BEDepreciacion obj = (BEDepreciacion) jcbval.getSelectedItem();
                } else if (jcbval.getSelectedIndex() == 0) {
                }
            }
        }
    }

    private void rellenarCategoria(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {
        BEDepreciacion be = null;
        BLDepreciacion bl = null;
        try {
            bl = new BLDepreciacion(config);
            jcbcat.removeAllItems();
            jcbanti.removeAllItems();
            jcbmat.removeAllItems();
            jcbest.removeAllItems();
            jcbval.removeAllItems();

            jcbcat.addItem(new BEDepreciacion());
            jcbanti.addItem(new BEDepreciacion());
            jcbmat.addItem(new BEDepreciacion());
            jcbest.addItem(new BEDepreciacion());
            jcbval.addItem(new BEDepreciacion());

            ArrayList<BEDepreciacion> list = bl.listarCategoria();
            for (int i = 0; i < list.size(); i++) {
                be = list.get(i);
                be.setTipo_item(BEDepreciacion.CATEGORIA);
                jcbcat.addItem(be);

            }
            jcbcat.setSelectedIndex(0);
            selectDefaultCategoria(jcbcat, jcbanti, jcbmat, jcbest, jcbval, cat, anti, mat, est, val);
        } catch (Exception e) {
            Logger.getLogger(UtilDepreciacion.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    private void rellenarAntiguedad(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {
        BEDepreciacion be = null;
        BLDepreciacion bl = null;
        try {
            jcbanti.removeAllItems();
            bl = new BLDepreciacion(config);
            be = (BEDepreciacion) jcbcat.getSelectedItem();
            jcbanti.removeAllItems();
            jcbanti.addItem(new BEDepreciacion());

            ArrayList<BEDepreciacion> list = bl.listarAntiguedadCategoria(be.getCategoria());
            for (int i = 0; i < list.size(); i++) {
                be = list.get(i);
                be.setTipo_item(BEDepreciacion.ANTIGUEDAD);
                jcbanti.addItem(be);
            }
            jcbmat.removeAllItems();
            jcbest.removeAllItems();
            jcbval.removeAllItems();
            jcbmat.addItem(new BEDepreciacion());
            jcbest.addItem(new BEDepreciacion());
            jcbval.addItem(new BEDepreciacion());

            jcbanti.setSelectedIndex(0);
            selectDefaultAntiguedad(jcbcat, jcbanti, jcbmat, jcbest, jcbval, cat, anti, mat, est, val);
        } catch (Exception e) {
            Logger.getLogger(UtilDepreciacion.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    private void rellenaMaterial(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {
        BEDepreciacion be = null;
        BEDepreciacion be1 = null;

        try {
            jcbmat.removeAllItems();
            BLDepreciacion bl = new BLDepreciacion(config);
            be = (BEDepreciacion) jcbcat.getSelectedItem();
            be1 = (BEDepreciacion) jcbanti.getSelectedItem();
            jcbmat.removeAllItems();
            jcbmat.addItem(new BEDepreciacion());
            ArrayList<BEDepreciacion> list = bl.listarMaterialAntiguedadCategoria(be.getCategoria(), be1.getAntiguedad());
            for (int i = 0; i < list.size(); i++) {
                be = list.get(i);
                be.setTipo_item(BEDepreciacion.MATERIAL);
                jcbmat.addItem(be);
            }
            jcbest.removeAllItems();
            jcbval.removeAllItems();
            jcbest.addItem(new BEDepreciacion());
            jcbval.addItem(new BEDepreciacion());
            jcbmat.setSelectedIndex(0);
            selectDefaultMaterial(jcbcat, jcbanti, jcbmat, jcbest, jcbval, cat, anti, mat, est, val);
        } catch (Exception ex) {
            Logger.getLogger(UtilDepreciacion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void rellenaEstado(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {
        BEDepreciacion be = null;
        BEDepreciacion be1 = null;
        BEDepreciacion be2 = null;
        try {
            jcbest.removeAllItems();
            BLDepreciacion bl = new BLDepreciacion(config);
            be = (BEDepreciacion) jcbcat.getSelectedItem();
            be1 = (BEDepreciacion) jcbanti.getSelectedItem();
            be2 = (BEDepreciacion) jcbmat.getSelectedItem();
            jcbest.removeAllItems();
            jcbest.addItem(new BEDepreciacion());
            ArrayList<BEDepreciacion> list = bl.listarEstadoMaterialAntiguedadCategoria(be.getCategoria(), be1.getAntiguedad(), be2.getMaterial());
            for (int i = 0; i < list.size(); i++) {
                be = list.get(i);
                be.setTipo_item(BEDepreciacion.ESTADO);
                jcbest.addItem(be);
            }
            jcbval.removeAllItems();
            jcbval.addItem(new BEDepreciacion());
            jcbest.setSelectedIndex(0);
            selectDefaultEstado(jcbcat, jcbanti, jcbmat, jcbest, jcbval, cat, anti, mat, est, val);
        } catch (Exception ex) {
            Logger.getLogger(UtilDepreciacion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void rellenaValorEstado(JComboBox jcbcat, JComboBox jcbanti, JComboBox jcbmat, JComboBox jcbest, JComboBox jcbval,
            String cat, String anti, String mat, String est, int val) {
        BEDepreciacion be = null;
        BEDepreciacion be1 = null;
        BEDepreciacion be2 = null;
        BEDepreciacion be3 = null;
        try {
            jcbval.removeAllItems();
            BLDepreciacion bl = new BLDepreciacion(config);
            be = (BEDepreciacion) jcbcat.getSelectedItem();
            be1 = (BEDepreciacion) jcbanti.getSelectedItem();
            be2 = (BEDepreciacion) jcbmat.getSelectedItem();
            be3 = (BEDepreciacion) jcbest.getSelectedItem();
            jcbval.removeAllItems();
            jcbval.addItem(new BEDepreciacion());
            ArrayList<BEDepreciacion> list = bl.listarValorEstadoMaterialAntiguedadCategoria(be.getCategoria(), be1.getAntiguedad(), be2.getMaterial(), be3.getEstado());
             for (int i = 0; i < list.size(); i++) {
                be = list.get(i);
                be.setTipo_item(BEDepreciacion.VALORESTADO);
                jcbval.addItem(be);
                System.out.println("VALOR ESTADO :" + be.getValorestado());
            }
            jcbval.setSelectedIndex(0);
            selectDefaultValorEstado(jcbcat, jcbanti, jcbmat, jcbest, jcbval, cat, anti, mat, est, val);
        } catch (Exception ex) {
            Logger.getLogger(UtilDepreciacion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getDefaultCategoria() {
        return defaultCategoria;
    }

    public void setDefaultCategoria(String defaultDepartamento) {
        this.defaultCategoria = defaultDepartamento;
    }

    public String getDefaultAntiguedad() {
        return defaultAntiguedad;
    }

    public void setDefaultAntiguedad(String defaultProvincia) {
        this.defaultAntiguedad = defaultProvincia;
    }

    public String getDefaultMaterial() {
        return defaultMaterial;
    }

    public void setDefaultMaterial(String defaultDistrito) {
        this.defaultMaterial = defaultDistrito;
    }

    public String getDefaultEstado() {
        return defaultEstado;
    }

    public void setDefaultEstado(String defaultEstado) {
        this.defaultEstado = defaultEstado;
    }

    public int getDefaultValorEstado() {
        return defaultValorEstado;
    }

    public void setDefaultValorEstado(int defaultValorEstado) {
        this.defaultValorEstado = defaultValorEstado;
    }

    public Config getConfig() {
        return config;
    }

    public void setConfig(Config config) {
        this.config = config;
    }
}
